package org.wizehack.mri.CoverageDataParser;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.wizehack.mri.test.TestConfigurator;

/**
 * The XMLFileReader represents java code coverage from a xml document exported from the EclEmma. 
 * The instance of the class loads the xml document and assigns the code coverage data to the instance of LineList class.   
 * @author hsyoun (wizehack@gmail.com)
 */

public class CoverageFileReader {

	private File xmlFile;
			
	/**
	 * 
	 * @param xmlFile the instance of xml file expored from the EclEma. 
	 *  
	 */
	public CoverageFileReader(File xmlFile){
		this.xmlFile = xmlFile;
	}
	
	/**
	 * example: </p>
	 *		File xmlFile = new File("./Data/jgraphx-1-1.xml");</p>
	 *		CoverageFileReader cfr = new CoverageFileReader(xmlFile);</p>
	 *				
	 *		SourceDir sd = cfr.getCoverage();</p>
	 *		
	 *		for(int i=0; i<sd.size(); i++){</p>
	 *			SourceFile sf = sd.get(i);</p>
			
	 *			System.out.println(sf.getName() + "---------------------------");</p>
	 *			for(int j=0; j<sf.size(); j++){</p>
	 *				System.out.println(sf.get(j).getLineNumber() + ": " + sf.get(j).isCovered());</p>
	 *			}</p>
	 *		}</p>
	 * 
	 * @return the coverage of source code 
	 */
	public List<SourceFile> getCoverage(){
//		SourceFileList srcDir = new SourceFileList();
		List<SourceFile> srcDir = new ArrayList<SourceFile>();
		
		XMLTreeTraverser xtt = new XMLTreeTraverser(this.xmlFile);
		NodeList nodeList = xtt.getNodeList("package");

		for(int i=0; i<nodeList.getLength(); i++){
			
			Element newRoot = (Element) nodeList.item(i);
			String pkgPath = TestConfigurator.getInstance().getSourceCodeDirLocation() + newRoot.getAttribute("name");
			
			NodeList filteredNodeList = newRoot.getElementsByTagName("sourcefile");	
			
			for(int j=0; j<filteredNodeList.getLength(); j++){
				
				Element sourceFileElement = (Element) filteredNodeList.item(j);
				
				SourceFile sf = scanLine(sourceFileElement, pkgPath);
				srcDir.add(sf);
				
			}
		}
		
		
//		for(int i=0; i<srcDir.size(); i++){
//			System.out.println(srcDir.get(i).getName());
//		}
		return srcDir;
	}
	
	private SourceFile scanLine(Element element, String dirPath){
		SourceFile srcFile = new SourceFile();
		String srcFilePath = dirPath + "/" + element.getAttribute("name");

		srcFile.setName(srcFilePath);		
		
		NodeList cNodeList = element.getChildNodes();
		
		for(int j=0; j < cNodeList.getLength(); j++){
			Node cNode = cNodeList.item(j);
			if(cNode.getNodeName().matches("line")){
				Element cElement = (Element) cNode; 
				Line line = new Line();
				line.setLineNumber(new Integer(cElement.getAttribute("nr")));
				
				if(cElement.getAttribute("mi").matches("0") ){
					line.setCovered(true);
				} else {
					line.setCovered(false);
				}
				
				srcFile.add(line);				
			} //end if
		}// end for i
		
		
		return srcFile;
	}
	
	/*
	public static void main(String[] args){
		
		File xmlFile = new File("/home/hsyoun/workspace/MRI-Demo/Data/6.xml");
		CoverageFileReader cfr = new CoverageFileReader(xmlFile);
				
		List <SourceFile> sd = cfr.getCoverage();
		
		for(int i=0; i<sd.size(); i++){
			SourceFile sf = sd.get(i);
			
//			System.out.println(sf.getName() + "---------------------------");
			for(int j=0; j<sf.size(); j++){
				System.out.println(sf.get(j).getLineNumber() + ": " + sf.get(j).isCovered());
			}
		}
		
	}
	*/
}
